package com.zhengdongzulin.zuke.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

class DoorLock : View {
    private lateinit var mPaint: Paint
    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private var mRadius: Int = 0

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        mWidth = width / 2
        mHeight = width / 2
        mRadius = width / 2
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.isDither = true
        mPaint.color = Color.parseColor("#color_1273f7")
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = 8f
        canvas.drawCircle(
            mWidth.toFloat(),
            mHeight.toFloat(),
            mRadius.toFloat(),
            mPaint
        )

        mPaint.strokeWidth = 4f
        canvas.drawCircle(
            mWidth.toFloat(),
            mHeight.toFloat(),
            mRadius / 2.toFloat(),
            mPaint
        )

        mPaint.strokeWidth = 16f
        mPaint.color = Color.parseColor("#0a7bcd")

        canvas.drawCircle(
            mWidth.toFloat(),
            mHeight.toFloat(),
            mRadius / 2 .toFloat() + 16 / 2,
            mPaint
        )


        //----------------------------------------------------------

        mPaint.color = Color.parseColor("#color_1273f7")
        mPaint.style = Paint.Style.FILL

        canvas.drawCircle(
            mWidth.toFloat(),
            mHeight.toFloat() * 0.9f,
            mRadius / 8f,
            mPaint
        )

        canvas.drawCircle(
            mWidth.toFloat(),
            mHeight.toFloat() * 1.1f,
            mRadius / 8f,
            mPaint
        )

        //大圆中间的大矩形
        mPaint.color = Color.parseColor("#color_1273f7")
        var rect = Rect()
        rect.left = mWidth - mRadius / 8
        rect.top = (mHeight * 0.9).toInt()
        rect.right = mHeight + mHeight / 8
        rect.bottom = (mHeight * 1.1).toInt()

        canvas.drawRect(rect, mPaint)


        //下圆锁
        mPaint.color = Color.parseColor("#FFFFFF")
        canvas.drawCircle(
            mWidth.toFloat(),
            mHeight.toFloat() * 1.1f,
            mRadius / 26f,
            mPaint
        )



        mPaint.color = Color.parseColor("#color_1273f7")
        //上矩形
        rect.left = mWidth + mRadius / 26
        rect.top = (mHeight * 0.9).toInt()
        rect.right = mWidth + mWidth / 6
        rect.bottom = mHeight

        canvas.drawRect(rect, mPaint)




        mPaint.color = Color.parseColor("#color_1273f7")
        //锁大圆
        canvas.drawCircle(
            mWidth + mWidth / 6f,
            mHeight.toFloat() * 0.9f + ((mRadius - mRadius * 0.9f) / 2),
            (mRadius  - mHeight * 0.9f) / 2 ,
            mPaint
        )

        mPaint.color = Color.parseColor("#FFFFFF")

        //锁小圆
        canvas.drawCircle(
            mWidth + mWidth / 6f,
            mHeight.toFloat() * 0.9f + ((mRadius - mRadius * 0.9f) / 2),
            (mRadius  - mHeight * 0.9f) / 2 / 2,
            mPaint
        )



        mPaint.color = Color.parseColor("#FFFFFF")
        //左小圆
        canvas.drawCircle(
            mWidth + mRadius / 26f,
            mHeight.toFloat() * 0.9f + ((mRadius - mRadius * 0.9f) / 2),
            (mRadius  - mHeight * 0.9f) / 2 / 2,
            mPaint
        )

        //锁内白色
        rect.left = mWidth + mRadius / 26
        rect.top = (mHeight.toFloat() * 0.9 + ((mRadius - mRadius * 0.9) / 2)).toInt() - ((mRadius  - mHeight * 0.9) / 2 / 2).toInt()
        rect.right = mWidth + mWidth / 6
        rect.bottom = (mHeight.toFloat() * 0.9 + ((mRadius - mRadius * 0.9) / 2)).toInt() + ((mRadius  - mHeight * 0.9) / 2 / 2).toInt()

        canvas.drawRect(rect, mPaint)

    }
}